namespace DotNetNuke.Entities.Users
{
    using DotNetNuke.Common;
    using DotNetNuke.Common.Utilities;
    using DotNetNuke.Data;
    using DotNetNuke.Entities.Portals;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Collections;
    using System.Web;

    public class UserOnlineController
    {
        public void ClearUserList()
        {
            string key = "OnlineUserList";
            DataCache.RemoveCache(key);
        }

        public int GetOnlineTimeWindow()
        {
            if (Globals.HostSettings.Contains("UsersOnlineTime"))
            {
                int GetOnlineTimeWindow;
                try
                {
                    return Convert.ToInt32(Globals.HostSettings["UsersOnlineTime"].ToString());
                }
                catch (Exception exception1)
                {
                    
                    Exception ex = exception1;
                    throw;
                    
                }
                return GetOnlineTimeWindow;
            }
            return 15;
        }

        public Hashtable GetUserList()
        {
            string key = "OnlineUserList";
            if (DataCache.GetCache(key) == null)
            {
                DataCache.SetCache(key, new Hashtable());
                Hashtable userList = (Hashtable) DataCache.GetCache(key);
            }
            return (Hashtable) DataCache.GetCache(key);
        }

        public bool IsEnabled()
        {
            return (Globals.HostSettings.Contains("DisableUsersOnline") && (Globals.HostSettings["DisableUsersOnline"].ToString() == "N"));
        }

        public void SetUserList(Hashtable userList)
        {
            string key = "OnlineUserList";
            DataCache.SetCache(key, userList);
        }

        private void TrackAnonymousUser(HttpContext context)
        {
            string cookieName = "DotNetNukeAnonymous";
            PortalSettings portalSettings = (PortalSettings) context.Items["PortalSettings"];
            if (portalSettings != null)
            {
                AnonymousUserInfo user;
                string userID;
                Hashtable userList = this.GetUserList();
                HttpCookie cookie = context.Request.Cookies[cookieName];
                if (cookie == null)
                {
                    userID = Guid.NewGuid().ToString();
                    cookie = new HttpCookie(cookieName);
                    cookie.Value = userID;
                    cookie.Expires = DateTime.Now.AddMinutes(20.0);
                    context.Response.Cookies.Add(cookie);
                    user = new AnonymousUserInfo();
                    user.UserID = userID;
                    user.PortalID = portalSettings.PortalId;
                    user.TabID = portalSettings.ActiveTab.TabID;
                    user.CreationDate = DateTime.Now;
                    user.LastActiveDate = DateTime.Now;
                    if (!userList.Contains(userID))
                    {
                        userList[userID] = user;
                    }
                }
                else if (cookie.Value == null)
                {
                    DateTime refDateTimeHelperS0 = new DateTime(0x7cf, 10, 12);
                    context.Response.Cookies[cookieName].Expires = refDateTimeHelperS0;
                }
                else
                {
                    userID = cookie.Value;
                    if (userList[userID] == null)
                    {
                        userList[userID] = new AnonymousUserInfo();
                        ((AnonymousUserInfo) userList[userID]).CreationDate = DateTime.Now;
                    }
                    user = (AnonymousUserInfo) userList[userID];
                    user.UserID = userID;
                    user.PortalID = portalSettings.PortalId;
                    user.TabID = portalSettings.ActiveTab.TabID;
                    user.LastActiveDate = DateTime.Now;
                    cookie = new HttpCookie(cookieName);
                    cookie.Value = userID;
                    cookie.Expires = DateTime.Now.AddMinutes(20.0);
                    context.Response.Cookies.Add(cookie);
                }
            }
        }

        private void TrackAuthenticatedUser(HttpContext context)
        {
            PortalSettings portalSettings = (PortalSettings) context.Items["PortalSettings"];
            if (portalSettings != null)
            {
                UserInfo objUserInfo = UserController.GetCurrentUserInfo();
                Hashtable userList = this.GetUserList();
                OnlineUserInfo user = new OnlineUserInfo();
                if (objUserInfo.UserID >= 0)
                {
                    user.UserID = objUserInfo.UserID;
                }
                user.PortalID = portalSettings.PortalId;
                user.TabID = portalSettings.ActiveTab.TabID;
                user.LastActiveDate = DateTime.Now;
                if (userList[objUserInfo.UserID.ToString()] == null)
                {
                    user.CreationDate = user.LastActiveDate;
                }
                userList[objUserInfo.UserID.ToString()] = user;
                this.SetUserList(userList);
            }
        }

        public void TrackUsers()
        {
            HttpContext context = HttpContext.Current;
            if (context.Items["CheckedUsersOnlineCookie"] == null)
            {
                context.Items["CheckedUsersOnlineCookie"] = "true";
                if (context.Request.IsAuthenticated)
                {
                    this.TrackAuthenticatedUser(context);
                }
                else if (context.Request.Browser.Cookies)
                {
                    this.TrackAnonymousUser(context);
                }
            }
        }

        public void UpdateUsersOnline()
        {
            Hashtable userList = this.GetUserList();
            DataProvider provider = DataProvider.Instance();
            Hashtable listToProcess = (Hashtable) userList.Clone();
            this.ClearUserList();
            try
            {
                provider.UpdateUsersOnline(listToProcess);
            }
            catch (Exception exception1)
            {
                
                Exception ex = exception1;
                
            }
            provider.DeleteUsersOnline(this.GetOnlineTimeWindow());
        }
    }
}

